<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
    	// $this->postyzm();
        $this->display();
    }

    public function postyzm($phone,$username,$shopname,$content){
      $Content_type='application/x-www-form-urlencoded';
       $accept='application/JSON';
      // 构造请求数据
      $url ="https://api.miaodiyun.com/20150822/industrySMS/sendSMS";
      $timestamp = date("YmdHis");
      $account_sid = "63e158d3cf114a17b9a101b2bda1983b"; // 主账户
      $auth_token = "ba1acb67661d4791968e814f1eb1ab37";
      $sig = md5($account_sid . $auth_token . $timestamp);
      $body= array("accountSid" => $account_sid, "timestamp" => $timestamp, "sig" => $sig, "respDataType"=> "JSON");
  // 在基本认证参数的基础上添加短信内容和发送目标号码的参数
      $body['smsContent'] ='【私宠订制】尊敬的：'.$username.'用户，'.$shopname.'提醒 '.$content.'，感谢您的支持。';
      $body['to'] = $phone;
      $headers = array('Content-type: ' . $Content_type, 'Accept: ' . $accept);
      // echo("url:<br/>" . $url . "\n");
      // echo("<br/><br/>body:<br/>" . json_encode($body));
      // echo("<br/><br/>headers:<br/>");
      // 要求post请求的消息体为&拼接的字符串，所以做下面转换
      $fields_string = "";
      foreach ($body as $key => $value) {
          $fields_string .= $key . '=' . $value . '&';
      }
      rtrim($fields_string, '&');
      // 提交请求
      $con = curl_init();
      curl_setopt($con, CURLOPT_URL, $url);
      curl_setopt($con, CURLOPT_SSL_VERIFYHOST, FALSE);
      curl_setopt($con, CURLOPT_SSL_VERIFYPEER, FALSE);
      curl_setopt($con, CURLOPT_HEADER, 0);
      curl_setopt($con, CURLOPT_POST, 1);
      curl_setopt($con, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($con, CURLOPT_HTTPHEADER, $headers);
      curl_setopt($con, CURLOPT_POSTFIELDS, $fields_string);
      $result = curl_exec($con);
      curl_close($con);
      return $result;
         // return $rand;
    }

    //登录
    public function login(){
      $username=I('username');
      $password= I('password');
      $info_admin=M('admin_user')->where("admin_u_tel=".$username)->find();
      if($info_admin){
        if($info_admin['admin_u_password']==md5($password)){
            $_SESSION['username']=$username;
            $this->ajaxReturn(100);  //验证成功
        }else{
            $this->ajaxReturn(102); //密码错误
        }
      }else{
        $this->ajaxReturn(101);    //没有此用户
      }
    }

    //用户列表页
    public function list_user(){
      // $_SESSION['username']='15902451269';
      $adminid   = $_SESSION['username'];
      $array_pid = M('admin')->where("admin_username=".$adminid)->field('admin_pid')->select();
      foreach ($array_pid as $key => $value) {
        $str_pid .= $array_pid[$key]['admin_pid'].',';
      }
      $array['id']=array('in',$str_pid);
      $petinfo_info= M('petinfo')->where($array)->field('petname,tel,id,ye,mo,da,addtime,pettype')->select();
      foreach ($petinfo_info as $key => $value) {
          $year=$value['ye']*3600*24*365;
          $money=$value['mo']*3600*24*30;
          $day=$value['da']*3600*24;
          $birthdaytime=$value['addtime']-($year+$money+$day);
          $petinfo_info[$key]['daynum']=(int)((time()-$birthdaytime)/(3600*24));
      }
      $this->assign('userinfo',$petinfo_info);
      $this->display();
    }

    public function send_messgs(){
      if(IS_POST){
         $id=I('id');
         $content=I('content');
         $id_str['id']=array('in',$id);
         $petinfo_info=M('petinfo')->where($id_str)->select();
         $shopname=M('petinfo')->where("shop_tel=".$_SESSION['username'])->getField('shopname');
         foreach ($petinfo_info as $key => $value) {
          $info=$this->postyzm($value['tel'],$value['petname'],$value['shopname'],$content);
         }
         $this->ajaxReturn(100);
      }else{
         $id=I('id');
         $id_str['id']=array('in',$id);
         $petinfo_info=M('petinfo')->where($id_str)->select();
         $shopname=M('petinfo')->where("shop_tel=".$_SESSION['username'])->getField('shopname');
         $this->assign('id',$id);
         $this->assign('info',$petinfo_info);
         $this->assign('shopname',$shopname);
         $this->display();
      }
         }
         //修改密码
    public function upPassword(){
      $this->display();
    }

     public function yzm(){
       $phone=I('phone');
       $wangji=I('wang');
        if(!preg_match("/^1[34578]\d{9}$/", $phone)){
             $data=10400;
             $this->ajaxReturn($data);
            }
        $where['tel']=$phone;
        if(empty($wangji)){
            if(M('user')->where($where)->find()){
              $date=10500;
              $this->ajaxReturn($date);
        }
        }
       $_SESSION['wang_photo']=$phone;
       $info=$this->postyzm_ver($phone);
       // $this->ajaxReturn($info);
       $infoall=json_decode($info);
       $yzminfo=htmlspecialchars_decode($infoall->respCode);
       $this->ajaxReturn($yzminfo);
       // $this->ajaxReturn($info);
    }

    public function postyzm_ver($phone){
      $rand=mt_rand(1000,9999);
      session(array($phone=>$rand,'expire'=>1800));
      $_SESSION[$phone]=$rand;
      $Content_type='application/x-www-form-urlencoded';
       $accept='application/JSON';
      // 构造请求数据
      $url ="https://api.miaodiyun.com/20150822/industrySMS/sendSMS";
      $timestamp = date("YmdHis");
      $account_sid = "63e158d3cf114a17b9a101b2bda1983b"; // 主账户
      $auth_token = "ba1acb67661d4791968e814f1eb1ab37";
      $sig = md5($account_sid . $auth_token . $timestamp);
      $body= array("accountSid" => $account_sid, "timestamp" => $timestamp, "sig" => $sig, "respDataType"=> "JSON");
  // 在基本认证参数的基础上添加短信内容和发送目标号码的参数
      $body['smsContent'] ='【宠物领养】您注册宠物领养网站的验证码为'.$rand.'，请于30分钟内正确输入验证码。';
      $body['to'] = $phone;
      $headers = array('Content-type: ' . $Content_type, 'Accept: ' . $accept);
      $fields_string = "";
      foreach ($body as $key => $value) {
          $fields_string .= $key . '=' . $value . '&';
      }
      rtrim($fields_string, '&');
      // 提交请求
      $con = curl_init();
      curl_setopt($con, CURLOPT_URL, $url);
      curl_setopt($con, CURLOPT_SSL_VERIFYHOST, FALSE);
      curl_setopt($con, CURLOPT_SSL_VERIFYPEER, FALSE);
      curl_setopt($con, CURLOPT_HEADER, 0);
      curl_setopt($con, CURLOPT_POST, 1);
      curl_setopt($con, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($con, CURLOPT_HTTPHEADER, $headers);
      curl_setopt($con, CURLOPT_POSTFIELDS, $fields_string);
      $result = curl_exec($con);
      curl_close($con);
      return $result;
    }

    public function ver_yzm(){
      $phone=I('phone');
      $yzm=I('yzm');
      if($_SESSION[$phone]==$yzm){
        $date['info']='验证成功';
        $date['status']=100;
        $_SESSION['username']=$phone;
        $this->ajaxReturn($date);
      }else{
        $date['info']='验证失败';
        $date['status']=101;
        $this->ajaxReturn($date);
      }
    }
//验证码 修改页面
    public function yzm_info(){
      $this->display();
    }
 //修改密码
   public function up_password(){
    if(empty($_SESSION['username'])){
      echo '非法进入';
      exit();
    }
    $password['admin_u_password']=md5(I('password'));
    $shop_tel=$_SESSION['username'];
    $admin_user=M('admin_user')->where("admin_u_tel=".$shop_tel)->save($password);
    if($admin_user!==false){
     $this->ajaxReturn(100);
    }elseif($admin_user==0){
     $this->ajaxReturn(101);
    }else{
     $this->ajaxReturn(102);
    }
   }   
 //应用列表
 public function useList(){
  $tel=$_SESSION['username'];
  $this->assign('tel',$tel);
  $this->display();
 }

}